package com.demo01_video.controller;

import com.demo01_video.bean.Video;
import com.demo01_video.bean.VideoDTO;
import com.demo01_video.repository.VideoRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/internal/videos")
@RequiredArgsConstructor
@Slf4j
public class VideoInternalController {

    private final VideoRepository videoRepository;

    @GetMapping("/{videoId}")
    public VideoDTO getVideoById(@PathVariable Long videoId) {
        return videoRepository.findById(videoId)
                .map(video -> {
                    VideoDTO dto = new VideoDTO();
                    dto.setId(video.getId());
                    dto.setTitle(video.getTitle());
                    dto.setAuthorId(video.getAuthorId());
                    dto.setVideoUrl(video.getVideoUrl());
                    dto.setCoverUrl(video.getCoverUrl());
                    dto.setStatus(video.getStatus());
                    dto.setLikeCount(video.getLikeCount());
                    dto.setCreatedAt(video.getCreatedAt());
                    return dto;
                })
                .orElseThrow(() -> new RuntimeException("视频不存在: " + videoId));
    }

    @GetMapping("/{videoId}/exists")
    public Boolean checkVideoExists(@PathVariable Long videoId) {
        return videoRepository.existsById(videoId);
    }

    @PostMapping("/{videoId}/update-like-count")
    public void updateLikeCount(@PathVariable Long videoId, @RequestParam int delta) {
        Video video = videoRepository.findById(videoId)
                .orElseThrow(() -> new RuntimeException("视频不存在: " + videoId));
        video.setLikeCount(video.getLikeCount() + delta);
        videoRepository.save(video);
    }
    
    /**
     * 获取用户最近发布的视频列表
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 视频列表
     */
    @GetMapping("/user/{userId}/recent")
    public List<VideoDTO> getRecentVideosByUser(
            @PathVariable Long userId, 
            @RequestParam int limit) {
        // 按创建时间倒序排列，获取最近的视频
        PageRequest pageRequest = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "createdAt"));
        return videoRepository.findByAuthorId(userId, pageRequest)
                .stream()
                .map(video -> {
                    VideoDTO dto = new VideoDTO();
                    dto.setId(video.getId());
                    dto.setTitle(video.getTitle());
                    dto.setAuthorId(video.getAuthorId());
                    dto.setVideoUrl(video.getVideoUrl());
                    dto.setCoverUrl(video.getCoverUrl());
                    dto.setStatus(video.getStatus());
                    dto.setLikeCount(video.getLikeCount());
                    dto.setCreatedAt(video.getCreatedAt());
                    return dto;
                })
                .collect(Collectors.toList());
    }
}